package com.alibaba.bos.action.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;



public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

    protected T model;
    @Override
    public T getModel() {
        return model;
    }
    
    public BaseAction(){
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
        try {
            model = modelClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            System.out.println("模型构造失败");
        }
    }

    
    //分页模型重构
    protected int page;
    protected int rows;
    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }
    
    public void pushPageDataToValueStack(Page<T> pageable){
        Map<String, Object> results=new HashMap<>();
        results.put("rows", pageable.getContent());
        results.put("total", pageable.getTotalElements());
        ActionContext.getContext().getValueStack().push(results);
    }
    
}
